package gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.LineBorder;

import main.Input;
import main.Terminal;
import controller.StatusSeat;
import controller.StatusSeat.STATUS;

public class UIShowSeats extends JFrame
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel cinemaPanel;
	private ImageIcon soldIcon;
	private ImageIcon freeIcon;
	private ImageIcon reservedIcon;
	private ImageIcon yourIcon;
	
	private ArrayList<StatusSeat> selectedSeats;
	private UISale ui;
	
	//CONSTANTS
	private final int SEAT_ICON_WIDTH = 32;
	private final int SEAT_ICON_HIGHT = 32;
	private final int SEAT_ICON_VERTICAL_SPACING = 10;
	private final int SEAT_ICON_HORIZONTAL_SPACING = 10;
	
	public UIShowSeats(UISale uiSale, ArrayList<StatusSeat> cinemaSeats)
	{
		this.ui = uiSale;
		selectedSeats = new ArrayList<StatusSeat>();
		
		initJFrame();
		initCinemaPanel();
		initStatusPanel();
		
		//load icons
		loadIcons();
	
		loadSeats(cinemaSeats);
		setVisible(true);
	}
	
	private void initJFrame()
	{
		getContentPane().setLayout(null);
		setBounds(100, 100, 960, 700);
		setVisible(true);
		addKeyListener(Input.getInstance());
		requestFocus();
	}
	
	private void initCinemaPanel()
	{
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 500, 600);
		getContentPane().add(scrollPane);
		cinemaPanel = new JPanel();
		scrollPane.setViewportView(cinemaPanel);
		cinemaPanel.setLayout(null);
		cinemaPanel.setBorder(new LineBorder(Color.BLACK));
	}
	
	private void initStatusPanel()
	{
		JPanel statusPanel = new JPanel();
		statusPanel.setBounds(520, 10, 270, 600);
		statusPanel.setBorder(new LineBorder(Color.BLACK));
		getContentPane().add(statusPanel);
		
		JButton btnAfslut = new JButton("Afslut");
		btnAfslut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				endSelection();
			}
		});
		btnAfslut.setBounds(800, 10, 89, 23);
		getContentPane().add(btnAfslut);
	}
	
	private void loadIcons() 
	{
		ClassLoader cl = ClassLoader.getSystemClassLoader();
		soldIcon = new ImageIcon(cl.getResource("images/soldSeat.png"));
		freeIcon = new ImageIcon(cl.getResource("images/freeSeat.png"));
		reservedIcon = new ImageIcon(cl.getResource("images/reservedSeat.png"));
		yourIcon = new ImageIcon(cl.getResource("images/yourSeat.png"));
	}

	
	private void loadSeats(ArrayList<StatusSeat> cinemaSeats)
	{
		int panelWidth = 0;
		int panelHeight = 0;
		
		for(StatusSeat statSeat : cinemaSeats) {
			
			//choose icon
			ImageIcon icon = null;
			switch(statSeat.getStatus()) {
				case FREE:
					icon = freeIcon;
					break;
				case RESERVED:
					icon = reservedIcon;
					break;
				case SOLD:
					icon = soldIcon;
					break;
			}
			
			//make label for the icon
			UISeat seatIcon = new UISeat(statSeat);
			seatIcon.setBorder(new LineBorder(Color.BLACK));
			seatIcon.setToolTipText(String.format("%d,%d", statSeat.getSeat().getRow(), statSeat.getSeat().getNumber()));
			seatIcon.setIcon(icon);
			
			//calculate position
			int row = statSeat.getSeat().getRow();
			int number = statSeat.getSeat().getNumber();
			int x = number * (SEAT_ICON_WIDTH + SEAT_ICON_HORIZONTAL_SPACING) - SEAT_ICON_WIDTH;
			int y = (row * SEAT_ICON_HIGHT) + (row * SEAT_ICON_VERTICAL_SPACING) - SEAT_ICON_HIGHT;
			seatIcon.setBounds(x, y, SEAT_ICON_WIDTH, SEAT_ICON_HIGHT);
			
			//add mouse listener (for selection)
			seatIcon.addMouseListener(new MouseHandler());
			
			//add to panel
			cinemaPanel.add(seatIcon);
			panelWidth += x;
			panelHeight += y;
		}
		
		cinemaPanel.setSize(panelWidth, panelHeight);
	}
	
	private void endSelection()
	{
		ui.setChosenSeats(selectedSeats);
		dispose();
	}
	

	/**
	 * Marks another seat adjacent to the current one. 
	 * @param addSeatRight	determines, if the new seat is to the right (true) or the left (false)
	 */
	public void addSeat(boolean addSeatRight) 
	{
		Terminal.say("Add seat to right: " + addSeatRight);
	}
	
	private class MouseHandler implements MouseListener
	{

		@Override
		public void mouseClicked(MouseEvent e) 
		{
			try {
				UISeat uis = (UISeat) e.getSource();
				StatusSeat clickedSeat = uis.getSeat();
				
				//is the clicked seat free?
				if(clickedSeat.getStatus() == STATUS.FREE) {
					
					//should it be de-selected?
					if(selectedSeats.contains(clickedSeat)) {
						uis.setIcon(freeIcon);
						selectedSeats.remove(clickedSeat);
					}
					//or selected?
					else {
						uis.setIcon(yourIcon);
						selectedSeats.add(clickedSeat);
					}
				} 
			}
			catch(Exception ex) {
				Terminal.say("No seat was clicked!", ex);
			}
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
		}

		@Override
		public void mousePressed(MouseEvent e) {
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			
		}
		
	}
}
